package db.user;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import o.Authority;
import o.UserType;
import db.Database;
import dbservice.user.UserDBService;
import po.Po;
import po.UserPo;
import tool.PasswordEncryptor;

public class UserDB implements UserDBService{
	private static final String creatTable = "CREATE TABLE IF NOT EXISTS `seac2`.`user` (`name` VARCHAR(45) NOT NULL,`password` VARCHAR(128) NOT NULL,`usertype` VARCHAR(45) NOT NULL,`authority` VARCHAR(45) NOT NULL,PRIMARY KEY (`name`),UNIQUE INDEX `name_UNIQUE` (`name` ASC));";

	public UserDB() throws SQLException {
		Connection c = Database.getConnection();
		Statement s = c.createStatement();
		s.execute(creatTable);
		ResultSet r=s.executeQuery("select * from user where name='admin';");
		if(!r.next()){
			UserPo admin=new UserPo();
			admin.setName("admin");
			admin.setAuthority(Authority.最高权限);
			admin.setPassword(new PasswordEncryptor().encrypt("admin"));
			admin.setUserType(UserType.管理员);
			add(admin);
		}
		s.close();
	}

	public UserPo get(UserPo user){
		UserPo po=new UserPo();
		Connection c=Database.getConnection();
		Statement s;
		try {
			s = c.createStatement();
			ResultSet r=s.executeQuery("select * from user where name='"+user.getName()+"';");
			if(r.next()){				
				po.setName(r.getString(1));
				po.setPassword(r.getString(2));
				po.setUserType(UserType.valueOf(r.getString(3)));
				po.setAuthority(Authority.valueOf(r.getString(4)));
				po.successed();
			}else{
				po.failed("用户不存在");
			}
			s.close();
		} catch (SQLException e) {
			po.failed(e.getMessage());
			e.printStackTrace();
		}
		return po;
		
	}
	
	public List<UserPo> getAll(){
		List<UserPo> list=new ArrayList<UserPo>();
		Connection c=Database.getConnection();
		try {			
			Statement s = c.createStatement();
			ResultSet r=s.executeQuery("select * from user;");
			while(r.next()){
				UserPo po=new UserPo();
				po.setName(r.getString(1));
				po.setPassword(r.getString(2));
				po.setUserType(UserType.valueOf(r.getString(3)));
				po.setAuthority(Authority.valueOf(r.getString(4)));
				po.successed();
				list.add(po);
			}
			s.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return list;
	}
	
	public Po add(UserPo user){
		Po po=new Po();
		Connection c=Database.getConnection();
		Statement s;
		try {
			s = c.createStatement();
			s.executeUpdate("insert into user values('"+user.getName()+"','"+user.getPassword()+"','"+user.getUserType().name()+"','"+user.getAuthority().name()+"');");
			s.close();
			po.successed();
		} catch (SQLException e) {
			po.failed(e.getMessage());
			e.printStackTrace();
		}
		return po;
	}
	

	public Po update(UserPo user){
		Po po=new Po();		
		Connection c=Database.getConnection();
		Statement s;
		try {
			if(user.getName()==null){
				throw new NullPointerException();
			}
			s = c.createStatement();
			if(user.getPassword()!=null){
				s.addBatch(("update user set password='"+user.getPassword()+"' where name='"+user.getName()+"';"));
			}
			if(user.getAuthority()!=null){
				s.addBatch(("update user set authority='"+user.getAuthority().name()+"' where name='"+user.getName()+"';"));
			}
			if(user.getUserType()!=null){
				s.addBatch(("update user set usertype='"+user.getUserType().name()+"' where name='"+user.getName()+"';"));
			}
			s.executeBatch();
			s.close();
			po.successed();
		} catch (SQLException e) {
			po.failed(e.getMessage());
			e.printStackTrace();
		}		
		return po;
	}
	
	public Po delete(UserPo user){
		Po po=new Po();
		Connection c=Database.getConnection();
		Statement s;
		try {
			s = c.createStatement();
			if(s.executeUpdate("delete from user where name='"+user.getName()+"';")==0){
				po.failed("用户不存在");
			}else{
				po.successed();
			}
			s.close();
		} catch (SQLException e) {
			po.failed(e.getMessage());
			e.printStackTrace();
		}		
		return po;
	}
}
